package com.xbongbong.saas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.pro.dingtalk.pojo.dto.BundlePerfomanceDateListDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundlePerfomanceListDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundlePerfomanceDateListVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundlePerfomanceGetVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundlePerfomanceListVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundlePerfomanceVO;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.DingBundleRankEntity;
import com.xbongbong.saas.domain.entity.DingRobotManageEntity;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.enums.mini.DingRobotManagePushEnum;
import com.xbongbong.saas.model.DingBundleRankModel;
import com.xbongbong.saas.model.DingRobotManageModel;
import com.xbongbong.saas.service.DingBundleRankService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/1 9:13 下午
 */
@Service("dingBundleRankService")
public class DingBundleRankServiceImpl implements DingBundleRankService {
    @Resource
    private DingBundleRankModel dingBundleRankModel;
    @Resource
    private DingRobotManageModel dingRobotManageModel;
    @Override
    public BundlePerfomanceListVO list(BundlePerfomanceListDTO bundlePerfomanceListDTO) throws XbbException {
        BundlePerfomanceListVO bundlePerfomanceListVO = new BundlePerfomanceListVO();
        List<BundlePerfomanceVO> list = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", bundlePerfomanceListDTO.getCorpid());
        param.put("openConversationId",bundlePerfomanceListDTO.getOpenConversationId());
        param.put("groupByStr","rank_time");
        param.put("orderByStrGroup","rank_time desc");
        param.put("startGroup", bundlePerfomanceListDTO.getPage());
        param.put("del",0);
        List<DingBundleRankEntity> rowsCount = dingBundleRankModel.findEntitys(param);
        PageHelper pageHelper = PageHelperUtil.getGroupPageHelper(param,rowsCount.size(),dingBundleRankModel,bundlePerfomanceListDTO.getPageSize());
        List<DingBundleRankEntity> dingBundleRankList = (List<DingBundleRankEntity>) PageHelperUtil.getGroupEntityList(param, pageHelper, dingBundleRankModel);
    
        dingBundleRankList.forEach(dingBundleRankEntity -> {
            BundlePerfomanceVO bundlePerfomanceVO = new BundlePerfomanceVO();
            Long rankTime = dingBundleRankEntity.getRankTime();
            String rankStr = DateTimeUtil.getDateStringEpochSecond(rankTime);
            String describe = String.format(I18nMessageUtil.getMessage(StringConstant.PERFORMANCE_RANK), dingBundleRankList.size());
            String rankStrLast = rankStr + describe;
            bundlePerfomanceVO.setTitle(rankStrLast);
            bundlePerfomanceVO.setDateStart(DateTimeUtil.getStartOfDay(rankTime));
            bundlePerfomanceVO.setDateEnd(DateTimeUtil.getEndOfDay(rankTime));
            list.add(bundlePerfomanceVO);
        });
        bundlePerfomanceListVO.setList(list);
        bundlePerfomanceListVO.setPageHelper(pageHelper);
        return bundlePerfomanceListVO;
    }
    
    @Override
    public BundlePerfomanceDateListVO getDayList(BundlePerfomanceDateListDTO bundlePerfomanceDateListDTO) throws XbbException {
        BundlePerfomanceDateListVO bundlePerfomanceDateListVO = new BundlePerfomanceDateListVO();
        String corpid = bundlePerfomanceDateListDTO.getCorpid();
        String openConversationId = bundlePerfomanceDateListDTO.getOpenConversationId();
        List<BundlePerfomanceGetVO> list = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        //为了兼容之前的错误，有结束时间，没有开始时间的一个bug
        Long dateStart = bundlePerfomanceDateListDTO.getDateStart();
        if (Objects.isNull(bundlePerfomanceDateListDTO.getDateStart()) && Objects.nonNull(bundlePerfomanceDateListDTO.getDateEnd())){
            dateStart = DateTimeUtil.getStartOfDay(bundlePerfomanceDateListDTO.getDateEnd());
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("openConversationId",openConversationId);
        param.put("orderByStr","sort asc");
        param.put("rankTimeStart",dateStart);
        param.put("rankTimeEnd",bundlePerfomanceDateListDTO.getDateEnd());
        param.put("page", bundlePerfomanceDateListDTO.getPage());
        param.put("del",0);
        PageHelper pageHelper = PageHelperUtil.getPageHelper(param, dingBundleRankModel, bundlePerfomanceDateListDTO.getPageSize());
        List<DingBundleRankEntity> dingBundleRankList = (List<DingBundleRankEntity>)PageHelperUtil.getEntityList(param,pageHelper,dingBundleRankModel);
        dingBundleRankList.forEach(dingBundleRankEntity -> {
            BundlePerfomanceGetVO bundlePerfomanceGetVO = new BundlePerfomanceGetVO();
            BeanUtil.copyProperties(dingBundleRankEntity, bundlePerfomanceGetVO);
            list.add(bundlePerfomanceGetVO);
        });
        DingRobotManageEntity dingRobotManageEntity = dingRobotManageModel.findEntity(openConversationId,DingRobotManagePushEnum.PERFORMANCE_RANKING.getAlias(),corpid);
        String staticTime = DingRobotManagePushEnum.PERFORMANCE_RANKING.getStaticsTime();
        if ( Objects.nonNull(dingRobotManageEntity) ) {
            staticTime = dingRobotManageEntity.getStatisticsDeadline();
        }
        bundlePerfomanceDateListVO.setStaticTime(staticTime);
        bundlePerfomanceDateListVO.setList(list);
        bundlePerfomanceDateListVO.setPageHelper(pageHelper);
        return bundlePerfomanceDateListVO;
    }
}
